define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var ajax = require("resource/js/component/ajax.js");
	var EUtil = require("resource/js/component/easyutil.js");
	var alertMsg = require("resource/js/component/alerts.js");

	var openImgsDlg = function(){
		var imgs = ["baoxiao.png","beiyj.png","caigou.png","chuchai.png","jiaban.png","lizhi.png","qingjia.png","ruzhi.png","tuandf.png","tyshenpi.png",
		            "waichu.png","yinzhangsy.png","zdfyw.png","zhaopin.png","zizhisy.png"];
		var html = "<div class='selectImgsDiv'>";
		for(var i=0;i<imgs.length;i++){
			html += "<div class='imgbox'><input type='hidden' value='/oa/act/approval/progress/images/"+imgs[i]+"'><img src='../../../../../oa/act/approval/progress/images/"+imgs[i]+"'/></div>";
		}
		html+="</div>";
		$('#imgDlg').dialog({
			title:'流程图片选择',
			content:html,
		    buttons:[{
		    	text:'确定',
				handler: function(rows){
					var img = $(".selected").find('input').val();
					if(img){
						$("#demoImg").attr('src',"../../../../.."+img);
						$("#iconUrl").val(img);
						$('#imgDlg').dialog('close');
					}else{
						$.messager.alert('系统消息','请选择流程图片');
					}
				}
			},
			{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				handler:function(){
					$('#imgDlg').dialog('close');
				}
			}]
		});
		$("#imgDlg").dialog("open");
		$(".imgbox").bind('click',function(){
			$(".selectImgsDiv").find(".imgbox").attr("class","imgbox");
			$(this).addClass("selected");
			$(this).find("input").attr('checked',true);
		});
	};
	function selectForms(){
		$("#formName_add").textbox({
			prompt: '请选择关联表单',
			editable:false,
			iconWidth: 22,
			icons: [{
				iconCls:'icon-security',
				handler: function(e){
					$('#formdlg').dialog({
						title:'表单列表',
						href:BASEPATH+"/view/oa/act/approval/progress/formlist",
						onLoad:function(){
							var dialogConfig = undefined;
							var dialogUtil = undefined;
							dialogConfig = {
									fitColumns:true,
									singleSelect:true,
									striped:true,
									autoRowHeight:false,
									autoLoad:true,
									fit: true,
									url:BASEPATH+'/oa/act/approval/form/queryFormByPage.htm',
									columns:[[
											      {field:'form_name',title:'表单名称',width:100,align:'center',height:50},
											      {field:'form_des',title:'描述',width:100,align:'center',height:50},
											      {field:'user_name',title:'创建人',width:100,align:'center',height:50},
											      {field:'createtime',title:'创建时间',width:100,align:'center',height:50}
											]],
									eid:"approval_forms"
							};
							dialogUtil = EUtil.ajaxDataGrid(dialogConfig);
						},
					    buttons:[{
					    	text:'确定',
							handler: function(rows){
								var chks = $('#approval_forms').datagrid('getChecked');
								if(chks.length < 0 || chks.length > 1){
									$.messager.show({ title:'系统提示', msg:'请选择一条表单信息！', timeout:5000, showType:'slide' });
									return;
								}
								else{
									$('#formId_add').val(chks[0].form_id);
									using('textbox',function(){
										$("#formName_add").textbox("setValue",chks[0].form_name);
									});
									$('#formdlg').dialog('close');
								}
							}
						},
						{
							text:'取消',
							addClass:'l-btn-red',iconCls:'icon-not',
							handler:function(){
								$('#formdlg').dialog('close');
							}
						}]
					});
					$("#formdlg").dialog("open");
				}
			}]
		});
	}
	function getFormData(){
		var data = $("#addform").serializeJson();
		var name = data.processNodeName;
//		var approved = data.markOfApproved;
//		var approving = data.markOfApproving;
//		var wait = data.markOfWait;
//		var isSeal = data.isSeal;
//		var isSign = data.isSign;
		var arry = [];
		if(typeof name === 'string'){
			var node = {
					"processNodeName":name
			}
			arry.push(node);
		}
		if(name instanceof Array){
			for(var i =0;i<name.length;i++){
				var node = {
						"processNodeName":name[i]
//						"markOfApproved":approved[i],
//						"markOfApproving":approving[i],
//						"markOfWait":wait[i],
//						"isSeal":isSeal[i],
//						"isSign":isSign[i]
				}
				arry.push(node);
			}
		}

		delete data.processNodeName;
//		delete data.markOfApproved;
//		delete data.markOfApproving;
//		delete data.markOfWait;
//		delete data.isSeal;
//		delete data.isSign;
		data.nodes = arry;
		return data;
	}
	var initThisPage = function(dlg){
		$("#selectImg").bind('click',function(){
			openImgsDlg();
		});
		selectForms();
		var nodeIndex = 1;
		$('#addNode').off('click');
		$('#addNode').on('click',function(){
			nodeIndex++;
			var html = '<tr>'
				+'<td><input style="width:300px;" class="easyui-textbox" type="text" value="第'+nodeIndex+'级节点" name="processNodeName"'
						+'data-options="prompt:\'请输入第'+nodeIndex+'级节点名称\',required:true,validType:\'maxlength[10]\'"/></td>'
//				+'<td><input style="width:150px;" class="easyui-textbox" type="text" name="markOfApproved" data-options="prompt:\'批准后标语\',required:true,validType:\'maxlength[10]\'" value="同意" /></td>'
//				+'<td><input style="width:150px;" class="easyui-textbox" type="text" name="markOfApproving" data-options="prompt:\'批准中标语\',required:true,validType:\'maxlength[10]\'" value="审批中" /></td>'
//				+'<td><input style="width:150px;" class="easyui-textbox" type="text" name="markOfWait" data-options="prompt:\'等待批准标语\',required:true,validType:\'maxlength[10]\'" value="待审批" /></td>'
//				+'<td><input style="width:150px;" class="easyui-combobox" name="isSeal" value="0" data-options="editable:false,required:true,data:[{text:\'否\',value:\'0\'},{text:\'是\',value:\'1\'}]"  /></td>'
//				+'<td><input style="width:150px;" class="easyui-combobox" name="isSign" value="0" data-options="editable:false,required:true,data:[{text:\'否\',value:\'0\'},{text:\'是\',value:\'1\'}]"  /></td>'
				+'<td><div class="icon-btn icon-btn-remove"><span>-</span></div></td></tr>';
			$("#nodesTab").append(html);
			$.parser.parse($("#nodesTab tr:last"));
		});

		$('#nodesTab').off('click', '.icon-btn-remove');
		$('#nodesTab').on('click',  '.icon-btn-remove', function() {
			$(this).parent().parent('tr').remove();
		});

		$("#saveAddBtn").linkbutton({
			iconCls: 'icon-ok',
			iconAlign:'left',
			onClick:function(){
				$("#saveAddBtn").linkbutton('disable');
				using(["form"],function(){
					if($("#addform").form("validate")){
						var procParam = getFormData();
						if(!procParam.nodes || procParam.nodes.length==0){
							alertMsg.show("节点信息错误");
							$("#saveAddBtn").linkbutton('enable');
							return false;
						}
						ajax.init().success(function(code,msg,data){
							var key = data;
							var param ={
									groupId:procParam.groupName,
									checkeds:[key]
							};
							ajax.init().success(function(code,msg,data){
								alertMsg.show(msg,function(){
									window.parent.$('.dlg-goback').click();
								});
							}).sync().contentType('application/json').post(BASEPATH+'/oa/act/processGroup/addProcessToGroup.htm',JSON.stringify(param));

						}).contentType('application/json')
						.post(BASEPATH+'/oa/act/approval/progress/saveProgress.htm',JSON.stringify(procParam))
						.error(function(code,msg,data){
							alertMsg.show(msg);
							$("#saveAddBtn").linkbutton('enable');
						});
					}else{
						$("#saveAddBtn").linkbutton('enable');
					}
				});
			}
		});
	};
	var getGroup =  function(){
		var sysTypeData=[];
		ajax.init().success(function(code,msg,data){
			if(code==0){
				for(var k in data){
					sysTypeData.push({
						'groupId': data[k].groupId,
						'groupName': data[k].groupName,
					});
				}
				$('#groupName').combobox({
				    valueField:'groupId',
				    textField:'groupName',
				    data:sysTypeData
				});
				$('#groupName').combobox('select', sysTypeData[0].groupId);

			}else{
				$.messager.show({
					title:'系统消息',
					msg:msg,
					timeout:5000,
					showType:'slide'
				});
			}
		}).post(BASEPATH+'/oa/act/processGroup/queryGroupByDept.htm');
	}

	module.exports={
		init:function(dlg){
			using(["datagrid","linkbutton","parser","textbox","combobox","dialog"],function(){
				initThisPage();
				getGroup();
			});
		}
	}
});
